package cn.iocoder.yudao.module.pickbook.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.checkin.PickbookCheckinDO;
import cn.iocoder.yudao.module.pickbook.service.checkin.PickbookCheckinService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "打卡 APP API")
@RestController
@RequestMapping("/pickbook/app/checkin")
@Validated
@Slf4j
public class PickbookAppCheckinController {

    @Resource
    private PickbookCheckinService checkinService;

    @PostMapping("/create")
    @Operation(summary = "创建打卡记录")
    public CommonResult<PickbookCheckinDO> createCheckin(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam @NotNull(message = "绘本ID不能为空") Long bookId,
            @RequestParam(required = false) Long chapterId,
            @RequestParam(required = false) Long classId,
            @RequestParam(required = false, defaultValue = "0") Integer readingTime,
            @RequestParam(required = false, defaultValue = "0") Integer pagesRead,
            @RequestParam(required = false) String notes) {
        
        PickbookCheckinDO checkin = checkinService.createCheckin(userId, bookId, chapterId, classId,
                readingTime, pagesRead, notes);
        return success(checkin);
    }

    @GetMapping("/stats")
    @Operation(summary = "获取打卡统计")
    public CommonResult<PickbookCheckinService.CheckinStats> getCheckinStats(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId) {
        
        PickbookCheckinService.CheckinStats stats = checkinService.getCheckinStats(userId);
        return success(stats);
    }

    @GetMapping("/list")
    @Operation(summary = "获取打卡记录列表")
    public CommonResult<PageResult<PickbookCheckinDO>> getCheckinList(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        PageResult<PickbookCheckinDO> result = checkinService.getUserCheckins(userId, pageNo, pageSize);
        return success(result);
    }

    @GetMapping("/calendar")
    @Operation(summary = "获取打卡日历")
    public CommonResult<List<LocalDate>> getCheckinCalendar(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam @NotNull(message = "开始日期不能为空") String startDate,
            @RequestParam @NotNull(message = "结束日期不能为空") String endDate) {
        
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        List<LocalDate> calendar = checkinService.getCheckinCalendar(userId, start, end);
        return success(calendar);
    }

    @GetMapping("/today-status")
    @Operation(summary = "获取今日打卡状态")
    public CommonResult<Boolean> getTodayCheckinStatus(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId) {
        
        boolean hasCheckedIn = checkinService.hasCheckedInToday(userId, LocalDate.now());
        return success(hasCheckedIn);
    }

    @GetMapping("/class-list")
    @Operation(summary = "获取班级打卡记录")
    public CommonResult<PageResult<PickbookCheckinDO>> getClassCheckinList(
            @RequestParam @NotNull(message = "班级ID不能为空") Long classId,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        PageResult<PickbookCheckinDO> result = checkinService.getClassCheckins(classId, pageNo, pageSize);
        return success(result);
    }

} 